home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / WAREZ 1.1 source Folder / warez ƒ / warez code ƒ / warez progress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  4.9 KB  |  213 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        warez progress.c
  4.  
  5. Purpose:    This module handles the progress bar and dealing with
  6.             events while the progress bar is up.    
  7.  
  8.  
  9. WAREZ -=- nostalgia isn't what it used to be
  10. Copyright ©1994, Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "Power.h"
  30. #include "program globals.h"
  31. #include "warez progress.h"
  32. #include "msg dialogs.h"
  33. #include "msg environment.h"
  34. #include "msg menus.h"
  35. #include "msg main.h"
  36.  
  37. enum
  38. {
  39.     progressDialogID = 210,
  40.     progressText = 1,
  41.     progressBar = 2
  42. };
  43.  
  44. DialogPtr        gProgressDlog;
  45.  
  46. static    Rect            box;
  47. static    unsigned long    curProgress;
  48. static    unsigned long    maxProgress;
  49.  
  50. static pascal void DrawProgressBar(WindowPtr theWindow, int item);
  51.  
  52. static pascal void DrawProgressBar(WindowPtr theWindow, int item)
  53. {
  54.     Rect                tempBox;
  55.     unsigned long        length;
  56.     unsigned long        width;
  57.     long double            temp;
  58.     
  59.     SetPort(theWindow);
  60.     
  61.     FrameRect(&box);
  62.     
  63.     length = box.right - box.left;
  64.     
  65.     width = length * curProgress;
  66.     if((width / length) != curProgress) {
  67.         temp = ((long double)curProgress) / ((long double)maxProgress);
  68.         width = temp * length;
  69.     } else {
  70.         width /= maxProgress;
  71.     }
  72.     
  73.     tempBox = box;
  74.     InsetRect(&tempBox, 1, 1);
  75.     tempBox.left += width;
  76.     FillRect(&tempBox, ltGray);
  77.     
  78.     tempBox = box;
  79.     InsetRect(&tempBox, 1, 1);
  80.     tempBox.right = tempBox.left + width - 1;
  81.     ForeColor(cyanColor);
  82.     PaintRect(&tempBox);
  83.     ForeColor(blackColor);
  84. }
  85.  
  86. DialogPtr OpenProgressDialog(unsigned long max, Str255 theTitle)
  87. {
  88.     int                itemType;
  89.     Handle            itemH;
  90.     Rect            otherBox;
  91.     
  92.     PositionDialog('DLOG', progressDialogID);
  93.     gProgressDlog = GetNewDialog(progressDialogID, 0L, (WindowPtr)-1L);
  94.     if(gProgressDlog == 0L)
  95.         return 0L;
  96.     
  97.     GetDItem(gProgressDlog, progressBar, &itemType, &itemH, &box);
  98.     SetDItem(gProgressDlog, progressBar, userItem + itemDisable, DrawProgressBar, &box);
  99.     
  100.     curProgress = 0;
  101.     maxProgress = max;
  102.     
  103.     SetWTitle((WindowPtr)gProgressDlog, theTitle);
  104.     
  105.     ShowWindow(gProgressDlog);
  106.     DrawDialog(gProgressDlog);
  107.     
  108.     UpdateProgressDialog(0);
  109.     
  110.     gInProgress=TRUE;
  111.     AdjustMenus();
  112.     DrawMenuBar();
  113.     
  114.     return gProgressDlog;
  115. }
  116.  
  117. void SetProgressText(Str255 p1, Str255 p2, Str255 p3, Str255 p4)
  118. {
  119.     Str255            totalStr;
  120.     unsigned char    i;
  121.     int                itemType;
  122.     Handle            itemH;
  123.     Rect            otherBox;
  124.     
  125.     totalStr[0]=0x00;
  126.     for (i=1; i<=p1[0]; i++)
  127.         totalStr[++totalStr[0]]=p1[i];
  128.     for (i=1; i<=p2[0]; i++)
  129.         totalStr[++totalStr[0]]=p2[i];
  130.     for (i=1; i<=p3[0]; i++)
  131.         totalStr[++totalStr[0]]=p3[i];
  132.     for (i=1; i<=p4[0]; i++)
  133.         totalStr[++totalStr[0]]=p4[i];
  134.     GetDItem(gProgressDlog, 1, &itemType, &itemH, &otherBox);
  135.     SetIText((ControlHandle)itemH, totalStr);
  136. }
  137.  
  138. void UpdateProgressDialog(unsigned long cur)
  139. {
  140.     curProgress = cur;
  141.     if(curProgress >= maxProgress)
  142.         curProgress = maxProgress-1;
  143.     
  144.     SetPort(gProgressDlog);
  145.     
  146.     DrawProgressBar(gProgressDlog, progressBar);
  147.     
  148.     if (gHasPowerManager)
  149.         IdleUpdate();
  150. }
  151.  
  152. void DismissProgressDialog(void)
  153. {
  154.     if (gProgressDlog!=0L)
  155.         DisposDialog(gProgressDlog);
  156.     gProgressDlog=0L;
  157.     gInProgress=FALSE;
  158.     AdjustMenus();
  159.     DrawMenuBar();
  160. }
  161.  
  162. #define TheCancelKey    '.'
  163.  
  164. Boolean DealWithOtherPeople(void)
  165. {
  166.     /* this is just a small useful function to see if the user has cancelled */
  167.     /* a lengthy operation with command-period; could come in handy, I suppose, */
  168.     /* in a somewhat bizarre set of circumstances... */
  169.     /* Note that this procedure will break under AUX */
  170.     /* Note also that this returns TRUE if there has been no attempt to cancel */
  171.     
  172.     Boolean            foundEvent;
  173.     EvQElPtr        eventQPtr;
  174.     QHdrPtr            eventQHdr;
  175.     char            thisChar;
  176.     long            isCmdKey;
  177.     EventRecord        event;
  178.     Boolean            notDoneYet;
  179.     
  180.     SetCursor(&arrow);
  181.     HiliteMenu(0);
  182.  
  183.     foundEvent=FALSE;
  184.     eventQHdr=GetEvQHdr();
  185.     eventQPtr=(EvQElPtr)(eventQHdr->qHead);
  186.     while ((eventQPtr!=0L) && (!foundEvent))
  187.     {
  188.         if (eventQPtr->evtQWhat==keyDown)
  189.         {
  190.             thisChar=(char)((eventQPtr->evtQMessage)&charCodeMask);
  191.             isCmdKey=(eventQPtr->evtQModifiers)&cmdKey;
  192.             if (isCmdKey!=0L)
  193.                 foundEvent=(thisChar==TheCancelKey);
  194.         }
  195.         if (!foundEvent)
  196.             eventQPtr=(EvQElPtr)(eventQPtr->qLink);
  197.     }
  198.     
  199.     notDoneYet=TRUE;
  200.     do
  201.     {
  202.         if (WaitNextEvent(everyEvent, &event, gIsInBackground ? 100 : 0, 0L))
  203.             DispatchEvents(event);
  204.         else
  205.             notDoneYet=FALSE;
  206.         if (event.what==nullEvent)
  207.             notDoneYet=FALSE;
  208.     }
  209.     while (notDoneYet);
  210.     
  211.     return !foundEvent;
  212. }
  213.